package com.ruoyi.jetLinks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.jetLinks.domain.NmEquipment;
import com.ruoyi.jetLinks.mapper.NmEquipmentMapper;
import com.ruoyi.jetLinks.service.INmEquipmentService;
import com.ruoyi.jetLinks.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 设备信息管理Service业务层处理
 * 
 * @author lxx
 * @date 2021-12-17
 */
@Service
public class NmEquipmentServiceImpl implements INmEquipmentService
{
    @Autowired
    private NmEquipmentMapper nmEquipmentMapper;

    /**
     * 查询设备信息管理
     * 
     * @param id 设备信息管理ID
     * @return 设备信息管理
     */
    @Override
    public NmEquipment selectNmEquipmentById(Long id) {
        return nmEquipmentMapper.selectById(id);
    }

    /**
     * 查询设备信息管理列表
     * 
     * @param nmEquipment 设备信息管理
     * @return 设备信息管理
     */
    @Override
    public List<NmEquipment> selectNmEquipmentList(NmEquipment nmEquipment) {
        LambdaQueryWrapper<NmEquipment> nmEquipmentQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotNull(nmEquipment.getEqpId())){
            nmEquipmentQueryWrapper.eq(NmEquipment::getEqpId,nmEquipment.getEqpId());
        }
        if(StringUtils.isNotNull(nmEquipment.getQrImage())){
            nmEquipmentQueryWrapper.isNotNull(NmEquipment::getQrImage);
        }
        if(StringUtils.isNotNull(nmEquipment.getEquipmentName())){
            nmEquipmentQueryWrapper.like(NmEquipment::getEquipmentName,nmEquipment.getEquipmentName());
        }
        if(StringUtils.isNotNull(nmEquipment.getInstallationUnit())){
            nmEquipmentQueryWrapper.like(NmEquipment::getInstallationUnit,nmEquipment.getInstallationUnit());
        }
        nmEquipmentQueryWrapper.orderByDesc(NmEquipment::getCreateTime).orderByAsc(NmEquipment::getEquipmentName);
        return nmEquipmentMapper.selectList(nmEquipmentQueryWrapper);
    }

    /**
     * 新增设备信息管理
     * 
     * @param nmEquipment 设备信息管理
     * @return 结果
     */
    @Override
    public int insertNmEquipment(NmEquipment nmEquipment) {
        nmEquipment.setCreateTime(new Date());
        return nmEquipmentMapper.insert(nmEquipment);
    }

    /**
     * 修改设备信息管理
     * 
     * @param nmEquipment 设备信息管理
     * @return 结果
     */
    @Override
    public int updateNmEquipment(NmEquipment nmEquipment)
    {
        return nmEquipmentMapper.updateById(nmEquipment);
    }

    /**
     * 批量删除设备信息管理
     * 
     * @param ids 需要删除的设备信息管理ID
     * @return 结果
     */
    @Override
    public int deleteNmEquipmentByIds(Long[] ids) {
        return nmEquipmentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备信息管理信息
     * 
     * @param id 设备信息管理ID
     * @return 结果
     */
    @Override
    public int deleteNmEquipmentById(Long id) {
        return nmEquipmentMapper.deleteById(id);
    }

    /**
     * 批量更新二维码
     *
     * @param nmEquipment 需要更新二维码
     * @return 结果
     */
    @Override
    public int uptateNmEquipmentQrCode(NmEquipment nmEquipment,String url) {
        String text = url+"/qrCode.html?id="+ nmEquipment.getEqpId();
        System.out.println(text);
        // 嵌入二维码的图片路径
        try {
            String imgPath = "images/qrcode.jpg";
            String fileName = IdUtils.fastUUID() + ".jpg";
            String avatarPat =  RuoYiConfig.getUploadPath()+"/"+DateUtils.datePath() + "/" + fileName;
            String destPath = url + "/profile/upload/"+DateUtils.datePath() + "/" + fileName;
            QRCodeUtil.encode(text, imgPath, avatarPat, true);
            nmEquipment.setQrImage(destPath);
            nmEquipmentMapper.updateById(nmEquipment);
        } catch (Exception e){
            e.printStackTrace();
        }

        return 1;
    }

}
